package com.zsk.shop.controller.gop;

import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.base.BizException;
import com.zsk.shop.common.base.BizExceptionEnum;
import com.zsk.shop.common.utils.EntityConvertUtils;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.entity.GopRole;
import com.zsk.shop.model.params.GopRoleAddParam;
import com.zsk.shop.model.params.GopRoleEditParam;
import com.zsk.shop.model.results.GopRoleResult;
import com.zsk.shop.service.GopMenuService;
import com.zsk.shop.service.GopRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色控制器
 *
 * @author fengshuonan
 * @Date 2017年2月12日21:59:14
 */
@RestController
@Api(tags = "Gop菜单角色管理")
@RequestMapping("/gop/role")
public class GopRoleController {

    @Resource
    private GopRoleService roleService;

    @Resource
    private GopMenuService menuService;

    /**
     * 获取角色列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @ApiOperation("获取角色列表")
    @PostMapping(value = "/list.do")
    @ApiImplicitParams({@ApiImplicitParam(name="limit"),@ApiImplicitParam(name = "page")})
    public BaseResponse<GopRoleResult> list() {
        return roleService.roleList();
    }

    /**
     * 角色新增
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @ApiOperation("角色新增")
    @PostMapping(value = "/add.do")
    public BaseResponse add(@RequestBody GopRoleAddParam param) {
        return this.roleService.addRole(param);
    }

    /**
     * 角色修改
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @ApiOperation("角色修改")
    @PostMapping(value = "/edit.do")
    public BaseResponse edit(@RequestBody GopRoleEditParam param) {
        return this.roleService.editRole(param);
    }

    /**
     * 删除角色
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @ApiOperation("删除角色")
    @GetMapping(value = "/remove.do")
    @ApiImplicitParam(name = "id", value = "角色id")
    public BaseResponse remove(@RequestParam Long roleId) {
        this.roleService.delRoleById(roleId);
        return BaseResponse.success();
    }

    /**
     * 查看角色
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @ApiOperation("查看角色")
    @GetMapping(value = "/view.do")
    @ApiImplicitParam(name = "id", value = "角色id")
    public BaseResponse view(Long roleId) {
        if (ToolUtil.isEmpty(roleId)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        GopRole role = this.roleService.getById(roleId);
        GopRoleResult mcpRoleResult = EntityConvertUtils.convertAToB(role, GopRoleResult.class);
        List<String> menuNames = menuService.getMenuNamesByRoleId(roleId);
        mcpRoleResult.setMenuNames(menuNames);
        return BaseResponse.success(mcpRoleResult);
    }

    /**
     * 配置权限
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @ApiOperation("配置权限")
    @GetMapping("/setAuthority.do")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id"),
            @ApiImplicitParam(name = "ids", value = "菜单ids, 以逗号相隔")})
    public BaseResponse setAuthority(@RequestParam("roleId") Long roleId, @RequestParam("ids") String ids) {
        if (ToolUtil.isOneEmpty(roleId)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        this.roleService.setAuthority(roleId, ids);
        return BaseResponse.success();
    }
}
